package org.jeecg.modules.orderForm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.BeanToMapUtils;
import org.jeecg.modules.orderForm.entity.AggregationQuery;
import org.jeecg.modules.orderForm.entity.AggregationResult;
import org.jeecg.modules.orderForm.entity.VisitorAggregationQuery;
import org.jeecg.modules.orderForm.entity.VisitorAggregationsResult;
import org.jeecg.modules.orderForm.service.IOrderFormService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author lixiao
 * @version 1.0
 * @date 2021/4/9 8:10
 */
@RestController
@RequestMapping("/orderForm/aggregations")
@Slf4j
public class OrderFormAggregationsController {
    @Autowired
    private IOrderFormService orderFormService;
    @Autowired
    private ISysUserService sysUserService;


    @GetMapping("/queryTotal")
    public Result<AggregationResult> queryTotal(AggregationQuery aggregationQuery, HttpServletRequest request) {
        Result<AggregationResult> result = new Result<>();
        this.handleQuery(aggregationQuery);
        Map<String, Object> queryMap = BeanToMapUtils.transBean2Map(aggregationQuery);
        AggregationResult aggregationResult = orderFormService.queryTotalAggregations(queryMap);
        if (Objects.isNull(aggregationResult)) {
            aggregationResult = new AggregationResult();
            aggregationResult.setCommission(0L);
            aggregationResult.setProfit(0L);
            aggregationResult.setTotalCost(0L);
            aggregationResult.setTotalTakeMoney(0L);
        }
        result.setResult(aggregationResult);
        return result;
    }

    @GetMapping("/queryTotalByMonth")
    public Result<List<AggregationResult>> queryTotalByMonth(AggregationQuery aggregationQuery, HttpServletRequest request) {
        Result<List<AggregationResult>> result = new Result<>();
        this.handleQuery(aggregationQuery);
        Map<String, Object> queryMap = BeanToMapUtils.transBean2Map(aggregationQuery);
        List<AggregationResult> aggregationResultList = orderFormService.queryAggregationsByMonth(queryMap);
        if (CollectionUtils.isEmpty(aggregationResultList)) {
            aggregationResultList = new ArrayList<>();
        }
        result.setResult(aggregationResultList);
        return result;
    }

    @GetMapping("/queryYearList")
    public Result<List<String>> queryYearList(AggregationQuery aggregationQuery, HttpServletRequest request){
        Result<List<String>> result = new Result<>();
        this.handleQuery(aggregationQuery);
        result.setResult(orderFormService.queryDistinctYearList());
        return result;
    }


    @GetMapping("/queryVisitorAggregationsList")
    public Result<IPage<VisitorAggregationsResult>> queryVisitorAggregationsList(VisitorAggregationQuery visitorAggregationQuery,
                                                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                                 HttpServletRequest request){
        Result<IPage<VisitorAggregationsResult>> result = new Result<>();
        Map<String,Object> queryMap = BeanToMapUtils.transBean2Map(visitorAggregationQuery);
        visitorAggregationQuery.setPageNo(pageNo);
        visitorAggregationQuery.setPageSize(pageSize);
        Page<VisitorAggregationsResult> page = new Page(pageNo,pageSize);
        if (StringUtils.isNotEmpty(visitorAggregationQuery.getUserName())){
            List<String> roleCodeList = sysUserService.getRole(visitorAggregationQuery.getUserName());
            if (CollectionUtils.isNotEmpty(roleCodeList)){
                String roleCode = roleCodeList.get(0);
                switch (roleCode) {
                    case "manager":
                        queryMap.put("takeManager",visitorAggregationQuery.getUserName());
                        break;
                    case "visitor":
                        queryMap.put("takeOrders",visitorAggregationQuery.getUserName());
                        break;
                    case "regulator":
                        queryMap.put("createBy",visitorAggregationQuery.getUserName());
                        break;
                    default:
                        break;
                }
            }
        }
        System.out.println("queryMap = " + queryMap);
        result.setResult(orderFormService.queryVisitorAggregations(page,queryMap));
        return result;
    }


    /**
     * @param aggregationQuery 查询参数
     * @author lixiao
     * @date 2021/4/9
     */
    private void handleQuery(AggregationQuery aggregationQuery) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String userName = StringUtils.isNotEmpty(aggregationQuery.getUserName()) ? aggregationQuery.getUserName() : sysUser.getUsername();
        List<String> roleList = sysUserService.getRole(userName);
        String roleCode = roleList.get(0);
        switch (roleCode) {
            case "manager":
                aggregationQuery.setTakeManager(userName);
                break;
            case "visitor":
                aggregationQuery.setTakeOrders(userName);
                break;
            case "regulator":
                aggregationQuery.setCreateBy(userName);
                break;
            default:
                break;
        }
    }


}
